import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'TextSizeWidget.dart';

class BaseViewBar extends PreferredSize {
  Widget childView;
  @override
  Size preferredSize;

  BaseViewBar({this.preferredSize, this.childView});

  @override
  Widget build(BuildContext context) {
    Widget current = childView;
    if (childView == null) {
      preferredSize = preferredSize ??  Size.fromHeight(50.0);
      current = LimitedBox(
        maxWidth: 0.0,
        maxHeight: 0.0,
        child: ConstrainedBox(constraints: const BoxConstraints.expand()),
      );
    }
    return current;
  }
}

class BaseTitleBar extends StatelessWidget {
  var key;
  String title;
  IconData leftIcon = Icons.arrow_back_ios;
  bool isShowLeft;
  String rightText;
  VoidCallback leftClick ;
  TextStyle textStyle;
  final VoidCallback rightClick;

  BaseTitleBar(this.title, {this.key, this.leftIcon, this.isShowLeft = true, this.leftClick, this.rightText, this.rightClick});

  @override
  Widget build(BuildContext context) {
    return AppBar(
      flexibleSpace: Container(
        /// 实现渐变色的效果
        decoration: BoxDecoration(
          color: Colors.white,
          gradient: LinearGradient(
            colors: [
              Color.fromRGBO(239, 239, 239, 1),
              Color.fromRGBO(255, 255, 255, 1),
              Color.fromRGBO(239, 239, 239, 1),
            ],
          ),
        ),
      ),
      title: FixedSizeText(this.title,style: TextStyle(color: Colors.black),),
      leading: isShowLeft == true ? IconButton(
        /// 左边图标，视情况而定，自己穿参数
        icon: Icon(this.leftIcon ?? Icons.arrow_back_ios),
        color: Colors.black,
        onPressed: () {
          if(leftClick == null){
            Get.back();
          }else{
            leftClick();
          };
        },
      ):null,
      brightness: Brightness.dark,
      elevation: 2.0,
      centerTitle: true,
      actions: <Widget>[
        /// 右边的 布局，自己可以添加，是一个widget的一个集合，自已需求添加即可，我这里写了一个Text，和text的点击事件，
        RightView(title: rightText == null ? '': rightText, rightClick: rightClick),
      ],
    );
  }
}

class BaseTwoBar extends StatelessWidget {
  var key;
  String title;
  IconData leftIcon = Icons.arrow_back_ios;
  bool isShowLeft;
  String rightText;
  VoidCallback leftClick ;
  TextStyle textStyle;
  final VoidCallback rightClick;

  BaseTwoBar(this.title, {this.key, this.leftIcon, this.isShowLeft = true, this.leftClick, this.rightText, this.rightClick});

  @override
  Widget build(BuildContext context) {
    return AppBar(
      flexibleSpace: Container(
        /// 实现渐变色的效果
        decoration: BoxDecoration(
          color: Colors.white,
          gradient: LinearGradient(
            colors: [
              Color.fromRGBO(239, 239, 239, 1),
              Color.fromRGBO(255, 255, 255, 1),
              Color.fromRGBO(239, 239, 239, 1),
            ],
          ),
        ),
      ),
      title: FixedSizeText(this.title,style: TextStyle(color: Colors.black)),
      leading: isShowLeft == true ? IconButton(
        /// 左边图标，视情况而定，自己穿参数
        icon: Icon(this.leftIcon ?? Icons.arrow_back_ios),
        color: Colors.black,
        onPressed: () {
          if(leftClick == null){
            Get.back();
          }else{
            leftClick();
          };
        },
      ):null,
      brightness: Brightness.dark,
      elevation: 2.0,
//      centerTitle: true,
      actions: <Widget>[
        /// 右边的 布局，自己可以添加，是一个widget的一个集合，自已需求添加即可，我这里写了一个Text，和text的点击事件，
        RightView(title: rightText == null ? '': rightText, rightClick: rightClick),
      ],
    );
  }
}

/// 右边的 布局，以及点击事件
class RightView extends StatelessWidget {
  String title;
  VoidCallback rightClick;

  RightView({this.title, this.rightClick});

  @override
  Widget build(BuildContext context) {
    var containView;
    if (title != Null) {
      containView = Container(
        alignment: Alignment.center,
        padding: EdgeInsets.all(10.0),
        child: GestureDetector(
          child: FixedSizeText(
            this.title,
            style: TextStyle(color: Colors.blue, fontSize: 18.0),
          ),
          onTap: this.rightClick,
        ),
      );
    } else {
      containView = FixedSizeText("");
    }
    return containView;
  }
}
